#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    // 初始化UI
    ui->setupUi(this);
    setWindowTitle("中冶焦耐焦炉加热及废气系统故障自动检测系统V1.0");

    /*
     * 进行sqlite数据库连接，而这个界面类，只需要进行sqlite的连接，界面类不需要进行sql server的连接
     * 根据QT的官方文档，连接之后，要open这个连接，无论是出现在if条件语句里还是常规语句里，要有这么一个
     * 过程
    */
    globalSqliteConnection = QSqlDatabase::addDatabase("QSQLITE", "ui_sqlite_connection");
    globalSqliteConnection.setDatabaseName("bfmt.db");
    bool sqliteOpenFlag = globalSqliteConnection.open();
    if(sqliteOpenFlag) {
        printf("success connect property sqlite database...\n");
    } else {
        printf("error connect property sqlite database...\n");
    }

    /*
     * 建立tagModel的view和model
     * TODO: 尚未建立eventModel的view，后期记得建立一下
    */
    tagModel = new QSqlTableModel(nullptr, globalSqliteConnection);
    tagModel->setTable("tag");
    tagModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    tagModel->select();



    tagModel->setHeaderData(0, Qt::Horizontal, QObject::tr("序号"));
    tagModel->setHeaderData(1, Qt::Horizontal, QObject::tr("测点名称"));
    tagModel->setHeaderData(2, Qt::Horizontal, QObject::tr("测点位号"));
    tagModel->setHeaderData(3, Qt::Horizontal, QObject::tr("测点描述"));
    tagModel->setHeaderData(4, Qt::Horizontal, QObject::tr("测点区域"));
    tagModel->setHeaderData(5, Qt::Horizontal, QObject::tr("测点所属燃烧室号"));
    tagModel->setHeaderData(6, Qt::Horizontal, QObject::tr("测点低限"));
    tagModel->setHeaderData(7, Qt::Horizontal, QObject::tr("测点高限"));
    tagModel->setHeaderData(8, Qt::Horizontal, QObject::tr("测点对应交换信号"));
    tagModel->setHeaderData(9, Qt::Horizontal, QObject::tr("测点状态"));
    tagModel->setHeaderData(10, Qt::Horizontal, QObject::tr("测点是否参与计算"));
    tagModel->setHeaderData(11, Qt::Horizontal, QObject::tr("测点是否正在计算"));

    ui->tagTable->setModel(tagModel);
    ui->tagTable->setWindowTitle(QObject::tr("Table Model (View 1)"));

    //建立exchange_tag_cycle的view和model
    exchangeTagCycleModel = new QSqlTableModel(nullptr, globalSqliteConnection);
    exchangeTagCycleModel->setTable("exchange_tag_cycle");
    exchangeTagCycleModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    exchangeTagCycleModel->select();

    exchangeTagCycleModel->setHeaderData(0, Qt::Horizontal, QObject::tr("1#红向(DI1_14)"));
    exchangeTagCycleModel->setHeaderData(1, Qt::Horizontal, QObject::tr("1#绿向(DI1_15)"));
    exchangeTagCycleModel->setHeaderData(2, Qt::Horizontal, QObject::tr("2#红向(DI2_14)"));
    exchangeTagCycleModel->setHeaderData(3, Qt::Horizontal, QObject::tr("2#绿向(DI2_15)"));

    ui->exchangeTagCycleTable->setModel(exchangeTagCycleModel);
    ui->tagTable->setWindowTitle(QObject::tr("Table Model (View 2)"));

    //初始化菜单按钮
    tag = new QAction("测点属性");
    event = new QAction("报警事件");
    setting = new QAction("系统配置");

    //添加
    ui->menubar->addAction(tag);
    ui->menubar->addAction(event);
    ui->menubar->addAction(setting);

    ui->tag->hide();
    ui->event->hide();
    ui->setting->hide();

    connect(tag, &QAction::triggered, this, &MainWindow::showMeTheTag);
    connect(event, &QAction::triggered, this, &MainWindow::showMeTheEvent);
    connect(setting, &QAction::triggered, this, &MainWindow::showMeTheSetting);

    // 初始化扫描线程和扫描类
    scanner = new ExchangeScanner(nullptr);
    scanner->moveToThread(&scanThread);
    /*
     * 线程的创建和销毁均需要对子线程的执行动作函数产生影响，产生影响的方式采用QT信号槽方式，不采用
     * callback方式，线程的创建对应循环扫描交换信号函数，线程的销毁对应循环扫描定时器销毁
    */
    connect(&scanThread, &QThread::started, scanner, &ExchangeScanner::startScan);
    connect(&scanThread, &QThread::finished, scanner, &ExchangeScanner::stopScan);

    // 启动扫描线程
    scanThread.start();

}

MainWindow::~MainWindow()
{

    //析构时，扫描线程退出，等待
    scanThread.quit();
    scanThread.wait();
    delete ui;
}

void MainWindow::showMeTheTag()
{
    ui->tag->show();
    ui->event->hide();
    ui->setting->hide();
}

void MainWindow::showMeTheEvent()
{
    ui->tag->hide();
    ui->event->show();
    ui->setting->hide();
}

void MainWindow::showMeTheSetting()
{
    ui->tag->hide();
    ui->event->hide();
    ui->setting->show();
}

